using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.IO;

using DotNetNuke;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Entities.Tabs;
using DotNetNuke.Entities.Modules.Actions;
using DotNetNuke.Security;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.Services.Localization;

using Common;

public partial class controls_tm_TypicalEnterpriseLogoList : System.Web.UI.UserControl
{
    public int myPortalId = Null.NullInteger;
    public int ShowList()
    {
        TabController loc_tabController = new TabController();
        TabInfo loc_tabInfo = new TabInfo();

        ModuleController loc_moduleController = new ModuleController();
        ModuleInfo loc_moduleInfo = new ModuleInfo();

        try
        {
            loc_tabInfo = loc_tabController.GetTabByName("enterprise", Common.consts.constSystemPortalId);
            loc_moduleInfo = loc_moduleController.GetModuleByDefinition(Common.consts.constSystemPortalId, "enterprise");
            string fileName;
            int numItem = Common.systemSettings.sysTypicalUserMaxRow * Common.systemSettings.sysTypicalUserPerRow;

            string mySQLCmd =
            "SELECT TOP " + numItem + " companyFunction AS description," +
            " a.userId,a.email,'' AS status," +
            " CASE WHEN userType & " + sysLibs.turnBitOn(0, Common.consts.constENTBitPos) + ">0" +
            " THEN a.companyName1 ELSE a.fullName END AS name," +
            " CASE WHEN a.userType & " + sysLibs.turnBitOn(0, Common.consts.constENTBitPos) + ">0" +
            " THEN a.companyAddress ELSE a.address1 END AS address,b.fileName " +
            " FROM tm_users a LEFT OUTER JOIN tm_user_file b ON a.userId = b.userId " +
            " WHERE(a.grade = '" + Common.consts.constTypicalGrade + "') " +
            " AND (a.usersStatus = '" + Common.consts.constStatusENABLE + "') " +
            " AND (a.userType & " + sysLibs.turnBitOn(0, Common.consts.constSYSBitPos) + "=0)" +
            " AND (b.fileType IS NULL OR b.fileType & " + sysLibs.turnBitOn(0, Common.consts.constLOGOFileTypeBitPos) + "> 0)" +
            " ORDER BY NEWID()";

            sysLibs.ShowDebugMsg(lblDebugMsg, mySQLCmd);
            SqlConnection conn = new SqlConnection(sysLibs.GetConnectionString);
            SqlDataAdapter daTmp = new SqlDataAdapter(new SqlCommand(mySQLCmd, conn));
            DataTable dataTbl = new DataTable();
            daTmp.Fill(dataTbl);
            if (dataTbl.Rows.Count<=0) return 0;
            conn.Close();
            ArrayList dataArr = new ArrayList();
            for (int idx = 0; idx < dataTbl.Rows.Count; idx++)
            {
                controls_logoList.myImgLinkInfo imgInfo = new controls_logoList.myImgLinkInfo();
                imgInfo.linkUrl = DotNetNuke.Common.Globals.NavigateURL(loc_tabInfo.TabID, "viewDetail", "mid=" + loc_moduleInfo.ModuleID, "userId=" + dataTbl.Rows[idx]["userId"].ToString());
                fileName = dataTbl.Rows[idx]["fileName"].ToString();
                string filePathName = sysLibs.GetUpLoadUserDir + "/" + sysLibs.GetHashString(fileName) + "/" + fileName;
                if (File.Exists(Server.MapPath(filePathName)))
                    imgInfo.imgPath = filePathName;
                else
                    imgInfo.imgPath = consts.constNoImageFileName;
                imgInfo.logoText = sysLibs.SplitToMultiLine(dataTbl.Rows[idx]["name"].ToString(),
                                   Common.systemSettings.sysNumberOfWordUnderLogo, Common.systemSettings.sysNumberOfLineUnderLogo);
                imgInfo.title = dataTbl.Rows[idx]["name"].ToString();
                imgInfo.description = sysLibs.SplitToMultiLine(dataTbl.Rows[idx]["description"].ToString(), 
                                               systemSettings.sysNumberOfWordInDescription, systemSettings.sysNumberOfLineInDescription);
                dataArr.Add(imgInfo);
            }
            Tm_LogoList.Display(dataArr, Common.systemSettings.sysTypicalUserPerRow,myPortalId);
            return dataTbl.Rows.Count; 
        }
        catch (Exception exc)
        {
            Exceptions.ProcessModuleLoadException(this, exc);
        }
        return 0;
    }
}
